/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

	PrintSample

		 UtilityFunctions.cpp 

			Functions used throughout the application.

 ------------------------------------------------------------------

 Copyright (c) 2010 Microsoft Corporation. All rights reserved.

 THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
 ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A 
 PARTICULAR PURPOSE.

 ------------------------------------------------------------------
*/
#include "stdafx.h"
#include "PrintSample.h"

/** 
	<module>UtilityFunctions.cpp</module>

	<summary>
		<p>Gets a string resource from the application's resource file.</p>
		<p>Allocates a buffer for that resource from the process' heap.</p>
		<p>Copies the resource into the allocated buffer.</p>
		<p>Returns a pointer to the new buffer.</p>
		</summary>
 
	<param name="hInstance">
		The program's instance handle.</param>

	<param name="textResourceId">
		The resource ID of the string resource to return.</param>

	<param name="userBuffer">
		A pointer to the buffer that is allocated and filled by this function.</param>

	<returns>
		<p>The length, in characters, of the returned string.</p>
		<p>If an error occurs, this function returns zero.</p>
		</returns>

	<remarks>
		The calling function is responsible for freeing the returned buffer. 
		HeapFree should be called to release the memory when the returned 
		string is no longer needed.
		</remarks>

*/
int 
GetAllocedResourceString (
		HINSTANCE hInstance, 
		int textResourceId, 
		LPWSTR *userBuffer
)
{
	LPWSTR		textString = NULL;
	LPWSTR		captionString = NULL;
	LPWSTR		text = NULL;
	int			textStringLength = 0;
	int			returnValue = 0;

	// Get pointer to string resource
	textStringLength = LoadString (
						hInstance, 
						textResourceId, 
						(LPWSTR)(&textString), 
						0L);

	if (textStringLength > 0)
	{	
		// textString points into the application's resource buffer, which
		//  contains all the resource strings for this application. The
		//  strings in this buffer are not zero terminated and so are 
		//  copied to a new buffer where the terminating null character
		//  can be added.

		// Allocate temp buffer for this string
		text = (LPWSTR)HeapAlloc (GetProcessHeap(),PRINT_SAMPLE_HEAP_FLAGS, (SIZE_T)((textStringLength + 1)*sizeof(WCHAR)));

		if (NULL != text)
		{
			// Copy character bytes from string resource to local buffer
			memcpy(text, textString, (size_t)(textStringLength * sizeof(WCHAR)));
		}
		if (NULL != userBuffer)
		{
			*userBuffer = text;
			returnValue = textStringLength;
		}
	}
	return textStringLength;
}


/** 
	<module>UtilityFunctions.cpp</module>

	<summary>
		Displays a message box using the strings from the program's resource file.
		</summary>
 
	<param name="hInstance">
		The program's instance handle.</param>

	<param name="hWnd">
		The parent window of the message box.</param>

	<param name="textResourceId">
		The resource ID of the string to display in the body of the message box.</param>

	<param name="captionResourceId">
		The resource ID of the string to display in the caption bar of the message box.</param>

	<param name="type">
		The message box flags to use in the call to MessageBox</param>

	<returns>
		<p>The value returned by the call to MessageBox.</p>
		<p>If an error occurs, the function returns zero.</p>
		</returns>

*/
int 
MessageBoxWithResourceString (
		HINSTANCE hInstance, 
		HWND hWnd, 
		int textResourceId, 
		int captionResourceId, 
		UINT type
)
{
	LPWSTR		textString = NULL;
	LPWSTR		captionString = NULL;
	LPWSTR		text = NULL;
	LPWSTR		caption = NULL;
	int			textStringLength = 0;
	int			captionStringLength = 0;
	int			returnValue = 0;

	// returns a read only pointer to the text string
	textStringLength = LoadString (
		hInstance, 
		textResourceId, 
		(LPWSTR)(&textString), 
		0);

	if (textStringLength > 0)
	{	
		// textString points into the application's resource buffer, which
		//  contains all the resource strings for this application. The
		//  strings in this buffer are not zero terminated and so are 
		//  copied to a new buffer where the terminating null character
		//  can be added.

		// Allocate temp buffer for this string
		text = (LPWSTR)HeapAlloc (GetProcessHeap(),PRINT_SAMPLE_HEAP_FLAGS, (SIZE_T)((textStringLength + 1)*sizeof(WCHAR)));
	}
	if (NULL != text)
	{
		// Copy character bytes from string resource to local buffer
		memcpy(text, textString, (size_t)(textStringLength * sizeof(WCHAR)));

		// TextString was found so get pointer to captionString
		captionStringLength = LoadString (hInstance, captionResourceId, (LPWSTR)(&captionString), 0);
	}

	if (captionStringLength > 0)
	{	
		// Allocate temp buffer for this string
		caption = (LPWSTR)HeapAlloc (GetProcessHeap(),PRINT_SAMPLE_HEAP_FLAGS, (SIZE_T)((captionStringLength + 1)*sizeof(WCHAR)));
	}

	if (NULL != text)
	{
		// Copy string resource to local buffer
		memcpy(caption, captionString, (size_t)(captionStringLength * sizeof(WCHAR)));
	}

	// If either one of the strings was not found, return 0 to indicate an error
	if ((NULL != text) && (NULL != caption))
	{
		returnValue = MessageBox (hWnd, text, caption, type);
	}

	// Free memory buffers used.
	if (NULL != text)
	{
		HeapFree (GetProcessHeap(),0L, (LPVOID)text);
	}

	if (NULL != caption)
	{
		HeapFree (GetProcessHeap(),0L, (LPVOID)caption);
	}

	return returnValue;
}